package com.jasiri.project.ui.process.page

import android.graphics.Color
import android.graphics.Typeface
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import android.view.LayoutInflater
import android.view.View
import com.jasiri.project.R
import com.jasiri.project.databinding.PageOcrNoDocumentsBinding
import com.jasiri.project.pro.ProFragment
import com.jasiri.project.ui.process.activity.ProcessActivity
import com.jasiri.project.utils.event.EventType
import com.jasiri.project.utils.event.track.TrackEventManager
import com.jasiri.project.utils.http.AppUrls
import com.jasiri.project.utils.http.AppUrls.addCommonParams
import com.jasiri.lib.baseadapter.JasiriBaseAdapterUtil.getColor
import com.jasiri.project.app.App
import com.jasiri.project.lib.dev.JasiriSysDataHelper
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import org.json.JSONObject

/**
 * Sikadwa
 * @Author wwdeng
 * @CreateTime: 2024/4/19 18:22
 * @Annotation：流程 - 证件照 - 没有证件
 */
class OcrNoDocumentsPage(): ProFragment(), CoroutineScope by MainScope() {

    private var _binding: PageOcrNoDocumentsBinding? = null
    private val binding get() = _binding ?: throw IllegalStateException(
        "Cannot access binding because it is null. Either the view hasn't been created yet, or it has already been destroyed."
    )
    private var stepItem: JSONObject? = null
    constructor(data: JSONObject?): this(){
        this.stepItem = data
    }
    override fun getRootView(inflater: LayoutInflater): View {
        _binding = PageOcrNoDocumentsBinding.inflate(inflater)
        return binding.root
    }

    override fun initView() {
        rootViewBinding.containerLayout.setBackgroundColor(Color.parseColor("#00000000"))
        rootViewBinding.containerLayout.setBackgroundColor(requireContext().getColor(R.color.transparent))
    }

    override fun initListener() {
        //下一步
        _binding?.btnNext?.setOnClickListener {
            val params = JSONObject().addCommonParams().apply {
                put("path",AppUrls.PATH.OCR_INFO)
                put("cardType","NONE")//证件类型（PASSPORT、IDCARD、DRIVING、FACE、NONE）
            }
            val act = context as ProcessActivity
            act.submitAction(params)
            TrackEventManager.trackEventNow(EventType.CLICK.CLICK_STEP_OCR_NO_DOCUMENTS_NEXT,"")
            //上报位置和陀螺仪数据
            JasiriSysDataHelper.onCheckSubmitDataByTypes(App.instance(), arrayListOf("T11","T12"),"No Document Page")
        }
    }

    override fun initData() {
        addEvent(String.format(EventType.PAGE.PAGE_STEP_OCR_XX_START,"NoDocuments"),"")
        stepItem?.let {
            val creditLimitAmount = it.optString("limitAmount")
            val showTipsKey1 = String.format(getString(R.string.process_ocr_upload_limit_add_tips_key1),creditLimitAmount)
            val showTipsKey2 = getString(R.string.process_ocr_upload_limit_add_tips_key2)
            val showTipsText = String.format(getString(R.string.process_ocr_upload_limit_add_tips),showTipsKey1,showTipsKey2)
            val startTipsIndex1 = showTipsText.indexOf(showTipsKey1)
            val startTipsIndex2 = showTipsText.indexOf(showTipsKey2)
            val creditLimitTipsSpannable = SpannableString(showTipsText)
            creditLimitTipsSpannable.setSpan(ForegroundColorSpan(getColor(R.color.FFFF7226)), startTipsIndex1, startTipsIndex1 + showTipsKey1.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
            creditLimitTipsSpannable.setSpan(ForegroundColorSpan(getColor(R.color.FFFF7226)), startTipsIndex2, startTipsIndex2 + showTipsKey2.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
            //加粗
            creditLimitTipsSpannable.setSpan(StyleSpan(Typeface.BOLD), startTipsIndex1, startTipsIndex1 + showTipsKey1.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
            creditLimitTipsSpannable.setSpan(StyleSpan(Typeface.BOLD), startTipsIndex2, startTipsIndex2 + showTipsKey2.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
            binding.textNoDocumentsTips.text = creditLimitTipsSpannable
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        addEvent(String.format(EventType.PAGE.PAGE_STEP_OCR_XX_END,"NoDocuments"),"")
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}